package com.baomibing.flow.execute;

import com.baomibing.flow.constant.NodeTypeEnum;
import com.baomibing.flow.model.*;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Map;

/**
 * Execution
 *
 * @author frog 2023/9/18 15:48
 * @version 1.0.0
 **/
@Data
@Accessors(chain = true)
public abstract class Execution {
    //执行人
    protected String actor;
    //节点类型
    private NodeTypeEnum nodeType;
    //变量信息
    protected Map<String, Object> variables = Maps.newHashMap();

    public abstract NodeTypeEnum getNodeType();

    public static Execution createByNodeType(NodeTypeEnum type) {
        Execution execution = null;
        if (type == NodeTypeEnum.Task) {
            execution = new TaskExecution();
        } else if (type == NodeTypeEnum.Fork) {
            execution = new ForkExecution();
        } else if (type == NodeTypeEnum.Start) {
            execution = new StartExecution();
        } else if (type == NodeTypeEnum.Join) {
            execution = new JoinExecution();
        } else if (type == NodeTypeEnum.Parallel) {
            execution = new ParallelExecution();
        } else if (type == NodeTypeEnum.End) {
            execution = new EndExecution();
        }
        return execution;
    }

    public static Execution createByNode(WorkNode node) {
        Execution execution = null;
        if (node instanceof TaskNode) {
            execution = new TaskExecution();
        } else if (node instanceof ForkNode) {
            execution = new ForkExecution();
        } else if (node instanceof StartNode) {
            execution = new StartExecution();
        } else if (node instanceof JoinNode) {
            execution = new JoinExecution();
        } else if (node instanceof ParallelNode) {
            execution = new ParallelExecution();
        } else if (node instanceof EndNode) {
            execution = new EndExecution();
        }
        return execution;
    }
}
